package cn.wolfcode.luowowo.comment.service;

import cn.wolfcode.luowowo.comment.domain.ItemShopComment;
import cn.wolfcode.luowowo.comment.query.IteamShopCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IItemShopCommentRepository;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service
public class ItemShopCommentServiceImpl implements IIteamShopCommentService {
    @Autowired
    private IItemShopCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public ItemShopComment save(ItemShopComment itemShopComment) {
        
        itemShopComment.setCreateTime(new Date());
        repository.save(itemShopComment);
        return itemShopComment;
    }



    @Override
    public Page query(IteamShopCommentQuery qo) {
        Query query = new Query();
        long total = template.count(query, ItemShopComment.class);
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(),
                Sort.Direction.DESC, "createTime");
        query.with(pageable);

        List<ItemShopComment> list = template.find(query, ItemShopComment.class);
        return new PageImpl(list,pageable,total);
    }

}
